package presentation.view.menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import util.Execucao;
import presentation.view.JanelaInicial;
import presentation.view.MainFrame;
import presentation.view.consulta.ConsultaEspecialidade;
import presentation.view.consulta.ConsultaListar;
import presentation.view.consulta.ConsultasMedico;
import presentation.view.funcionario.FuncionarioListar;
import presentation.view.medico.MedicoListar;
import presentation.view.paciente.PacienteListar;
import presentation.view.relatorios.RelatoriosEspecialidade;
import presentation.view.relatorios.RelatoriosMedico;

public class MenuPrincipal extends JMenuBar implements ActionListener {    
    private JMenu cadastro;
    private JMenu consultas;
    private JMenu relatorio;

    //Itens do menu de cadastro
    private JMenuItem pacientes;
    private JMenuItem medicos;
    private JMenuItem funcionarios;
    private JMenuItem admins;
    
    //Itens do menu consultas
    private JMenuItem marcar;
    private JMenuItem visualizar;
    private JMenuItem realizar;

    //Itens do menu de relatorio
    private JMenuItem relatorioDeMedico;
    private JMenuItem relatorioDeEspecialidade;

    public MenuPrincipal(){
	if(Execucao.user.getTipo() == 0 || Execucao.user.getTipo() == 1){
	    cadastro = new JMenu("Cadastro");
	    pacientes = new JMenuItem("Pacientes");
	    pacientes.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.ALT_MASK));
	    pacientes.addActionListener(this);
	    cadastro.add(pacientes);
	    
	    funcionarios = new JMenuItem("Funcionarios");
	    funcionarios.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.ALT_MASK));
	    funcionarios.addActionListener(this);
	    cadastro.add(funcionarios);
	    
	    medicos = new JMenuItem("Médicos");
	    medicos.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));
	    medicos.addActionListener(this);
	    cadastro.add(medicos);
	    
	    if(Execucao.user.getTipo() == 0){	    
		admins = new JMenuItem("Admins");
		admins.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK));
		admins.addActionListener(this);
		cadastro.add(admins);
	    }
	    this.add(cadastro);
	}

	//TODO Fazer menu de consultas
	consultas = new JMenu("Consultas");
	if(Execucao.user.getTipo() == 2){
	    realizar = new JMenuItem("Realizar");
	    realizar.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.ALT_MASK));
	    realizar.addActionListener(this);
	    consultas.add(realizar);
	}else{
	    marcar = new JMenuItem("Marcar");
	    marcar.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.ALT_MASK));
	    marcar.addActionListener(this);
	    consultas.add(marcar);
	    
	    visualizar = new JMenuItem("Visualizar");
	    visualizar.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.ALT_MASK));
	    visualizar.addActionListener(this);
	    consultas.add(visualizar);
	}
	
	this.add(consultas);
	
	if(Execucao.user.getTipo() == 0 || Execucao.user.getTipo() == 1){
	    relatorio = new JMenu("Relatorios");

	    relatorioDeMedico = new JMenuItem("Relatório do Médico");
	    relatorioDeMedico.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.ALT_MASK));
	    relatorioDeMedico.addActionListener(this);
	    relatorio.add(relatorioDeMedico);

	    relatorioDeEspecialidade = new JMenuItem("Relatório da Especialidade");
	    relatorioDeEspecialidade.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.ALT_MASK));
	    relatorioDeEspecialidade.addActionListener(this);
	    relatorio.add(relatorioDeEspecialidade);
	    
	    this.add(relatorio);
	}
    }

    public void actionPerformed(ActionEvent e) {
	JMenuItem source = (JMenuItem)(e.getSource());
	if( source == pacientes){
	    MainFrame.getInstance().alterarPainel(new PacienteListar(), JanelaInicial.getInstance());
	} else if ( source == medicos){
	    MainFrame.getInstance().alterarPainel(new MedicoListar(), JanelaInicial.getInstance());
	} else if ( source == funcionarios){
	    MainFrame.getInstance().alterarPainel(new FuncionarioListar(false), JanelaInicial.getInstance());
	} else if ( source == admins){
	    MainFrame.getInstance().alterarPainel(new FuncionarioListar(true), JanelaInicial.getInstance());
	} else if ( source == marcar){
	    MainFrame.getInstance().alterarPainel(new ConsultaEspecialidade(), JanelaInicial.getInstance());
	} else if ( source == visualizar){
	    MainFrame.getInstance().alterarPainel(new ConsultaListar(), JanelaInicial.getInstance());
	} else if ( source == realizar){
	    MainFrame.getInstance().alterarPainel(new ConsultasMedico(), JanelaInicial.getInstance());
	} else if ( source == relatorioDeMedico){
	    MainFrame.getInstance().alterarPainel(new RelatoriosMedico(), JanelaInicial.getInstance());
	} else if ( source == relatorioDeEspecialidade){
	    MainFrame.getInstance().alterarPainel(new RelatoriosEspecialidade(), JanelaInicial.getInstance());
	}
	
    }
	
}
